package com.dale.controller;

/**
 * @author Gyt
 * @create 2021-06-05 - 10:24
 */

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dale.common.dto.QuesDto;
import com.dale.common.lang.Result;
import com.dale.entity.Answer;
import com.dale.entity.Ques;
import com.dale.entity.User;
import com.dale.service.AnswerService;
import com.dale.service.QuesService;
import com.dale.util.SensitiveFilter;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 专注 问答广场
 */
@RestController
public class QuesController {

    @Autowired
    QuesService quesService;

    @Autowired
    AnswerService answerService;

    //增加问题  目前仅支持管理员提问
    @RequiresAuthentication
    @RequiresRoles("admin")
    @PostMapping("/addQues")
    public Result addQues(@RequestBody Ques ques) {

        //敏感词检测替换
        String s = SensitiveFilter.replaceWords(ques.getQuestion());
        ques.setQuestion(s);

        //前端只传来ques的问题
        ques.setCreated(LocalDateTime.now());
        ques.setAnswerCount(0);

        quesService.save(ques);
        return Result.succ(null);
    }

    //增加回答 还是要个登陆吧
    @RequiresAuthentication
    @PostMapping("/addAnswer")
    public Result addAnswer(@RequestParam Long qid, @RequestBody Answer answer) {

        //敏感词检测替换
        String s = SensitiveFilter.replaceWords(answer.getAnswer());
        answer.setAnswer(s);

        //前端只传来回答
        answer.setCreated(LocalDateTime.now());
        answer.setQid(qid);

        Ques byId = quesService.getById(qid);
        //回答数加1
        byId.setAnswerCount(byId.getAnswerCount() + 1);
        quesService.updateById(byId);

        answerService.save(answer);

        return Result.succ(null);
    }


    //展示最新问题
    //只展示最近20条哈 不做分页了
    @GetMapping("/getQues")
    public Result getQues() {
        List<Ques> quesList = quesService.list(new QueryWrapper<Ques>().orderByDesc("created").last("limit 0,20"));

        ArrayList<QuesDto> quesDtos = new ArrayList<>();

        for (Ques ques : quesList) {
            QuesDto quesDto = new QuesDto();
            BeanUtil.copyProperties(ques, quesDto);
            quesDto.setAnswers((List<Answer>) getAnswer(ques.getQid()).getData());
            quesDtos.add(quesDto);
        }
        return Result.succ(quesDtos);
    }

    //展示最久问题
    //只展示最近20条哈 不做分页了
    @GetMapping("/getQuesOld")
    public Result getQuesOld() {
        List<Ques> quesList = quesService.list(new QueryWrapper<Ques>().orderByAsc("created").last("limit 0,20"));

        ArrayList<QuesDto> quesDtos = new ArrayList<>();

        for (Ques ques : quesList) {
            QuesDto quesDto = new QuesDto();
            BeanUtil.copyProperties(ques, quesDto);
            quesDto.setAnswers((List<Answer>) getAnswer(ques.getQid()).getData());
            quesDtos.add(quesDto);
        }
        return Result.succ(quesDtos);
    }

    //展示回答数最多
    //只展示最近20条哈 不做分页了
    @GetMapping("/getQuesMostAnswers")
    public Result getQuesMostAnswers() {
        List<Ques> quesList = quesService.list(new QueryWrapper<Ques>().orderByDesc("answer_count").last("limit 0,20"));

        ArrayList<QuesDto> quesDtos = new ArrayList<>();

        for (Ques ques : quesList) {
            QuesDto quesDto = new QuesDto();
            BeanUtil.copyProperties(ques, quesDto);
            quesDto.setAnswers((List<Answer>) getAnswer(ques.getQid()).getData());
            quesDtos.add(quesDto);
        }
        return Result.succ(quesDtos);
    }


    //展示回答
    @GetMapping("/getAnswer")
    public Result getAnswer(@RequestParam Long qid) {
//        Page page = new Page(currentPage, 5);
//        IPage pageData = answerService.page(page,
//                new QueryWrapper<Answer>()
//                        .orderByDesc("Created")
//                        .eq("qid", qid));
//        return Result.succ(pageData);
        List<Answer> list = answerService.list(new QueryWrapper<Answer>().orderByDesc("created").eq("qid", qid).last("limit 0,6"));
        return Result.succ(list);
    }


    //删除问题
    @RequiresAuthentication
    @RequiresRoles("admin")
    @DeleteMapping("/delQues")
    public Result delQues(@RequestParam Long qid) {

        Ques byId = quesService.getById(qid);

        Assert.isTrue(byId != null, "删除问题失败 该问题不存在");
        quesService.removeById(qid);

        return Result.succ(null);
    }

    //删除回答  回答数要减1

    @RequiresAuthentication
    @RequiresRoles("admin")
    @DeleteMapping("/delAnswer")
    public Result delAnswer(@RequestParam Long aid) {

        Answer byId = answerService.getById(aid);

        Assert.isTrue(byId != null, "删除回答失败 该回答不存在");

        //回答数要减1
        Ques byId1 = quesService.getById(byId.getQid());
        quesService.updateById(byId1.setAnswerCount(byId1.getAnswerCount() - 1));

        answerService.removeById(aid);

        return Result.succ(null);
    }
}
